package com.s64.ch08.modules.token;

import com.s64.ch08.entity.User;
import com.s64.ch08.modules.security.LoginUser;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.Base64Codec;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Primary
public class JwtTokenService implements TokenService {
    private static final String secretKey = "123@Af^.ax";
    @Override
    public Token createToken(LoginUser loginUser) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", "" + loginUser.getUser().getUserId());
        map.put("permsList", loginUser.getPermsList());
        map.put("roleKeyList", loginUser.getRoleKeyList());
        String jwt = Jwts.builder()
                .setClaims(map)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 10))
                .signWith(SignatureAlgorithm.HS256, Base64Codec.BASE64URL.encode(secretKey))
                .compact();
        return new Token(jwt, loginUser.getUser().getNickName());
    }

    @Override
    public boolean removeToken(String token) {
        return false;
    }

    @Override
    public LoginUser getLoginUser(String token) {
        LoginUser loginUser = new LoginUser();
        Claims body = Jwts.parser()
                .setSigningKey(Base64Codec.BASE64URL.encode(secretKey))
                .parseClaimsJws(token)
                .getBody();
        String userId = (String) body.get("userId");
        Set<String> permsList = new HashSet<String>((List)body.get("permsList"));
        Set<String> roleKeyList = new HashSet<String>((List)body.get("roleKeyList"));
        User user = new User();
        user.setUserId(Long.valueOf(userId));
        loginUser.setUser(user);
        loginUser.setPermsList(permsList);
        loginUser.setRoleKeyList(roleKeyList);
        return loginUser;
    }

    public static void main(String[] args) {
        LoginUser loginUser = new LoginUser();
        loginUser.setPermsList(new HashSet<>(Arrays.asList("user:list", "user:add")));
        loginUser.setRoleKeyList(new HashSet<>(Arrays.asList("admin")));
        User user = new User();
        user.setNickName("张三");
        user.setUserId(1L);
        loginUser.setUser(user);
        JwtTokenService jwtTokenService = new JwtTokenService();
        Token token = jwtTokenService.createToken(loginUser);
        System.out.println("token.getNickName() = " + token.getNickName());
        System.out.println("token.getToken() = " + token.getToken());

        LoginUser loginUser2 = jwtTokenService.getLoginUser(token.getToken());
        System.out.println("loginUser2.getUser().getUserId() = " + loginUser2.getUser().getUserId());
        System.out.println("loginUser2.getPermsList() = " + loginUser2.getPermsList());
        System.out.println("loginUser2.getRoleKeyList() = " + loginUser2.getRoleKeyList());
    }
}
